<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
    <div class="content">
        <div class="panel panel-default panel-intro">

            <div class="panel-body">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane fade active in" id="one">
                        <div class="widget-body no-padding">

                            <div class="bootstrap-table">
                                <div class="commonsearch-table ">
                                    <fieldset>
                                        <div class="row">
                                            <div class="form-group">
                                                <label for="c-thumb" class="control-label col-xs-12 col-sm-2">{:__('Image')}:</label>
                                                <div class="col-xs-12 col-sm-8">
                                                    <div class="input-group">
                                                        <input id="c-thumb" class="form-control" size="50" name="image" type="text" value="{$row['image']}"  disabled/>
                                                        <div class="input-group-addon no-border no-padding">
                                                            <span>
                                                                <button type="button" id="plupload-imagethumb" class="btn btn-danger plupload" data-input-id="c-thumb" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/webp" data-multiple="true" data-preview-id="p-thumb">
                                                                <i class="fa fa-upload"></i>上传</button>
                                                            </span>
                                                        </div>
                                                        <span class="msg-box n-right"></span>
                                                    </div>
                                                    <ul class="row list-inline plupload-preview" id="p-thumb" data-template="customtpl"></ul>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="control-label col-xs-12 col-sm-2">{:__('Title')}:</label>
                                                <div class="col-xs-12 col-sm-8">
                                                    <input id="c-title" data-rule="required" class="form-control" name="row[title]" value="{$row['title']}" type="text" disabled>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="control-label col-xs-12 col-sm-2">{:__('Brands_id')}:</label>
                                                <div class="col-xs-12 col-sm-8">
                                                    <input id="c-brands_id" data-rule="required" data-source="supplier/brands/index" data-field="name" class="form-control selectpage" name="row[brands_id]" type="text" value="{$row.brands_id|htmlentities}" disabled>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="control-label col-xs-12 col-sm-2">{:__('Place_origin')}:</label>
                                                <div class="col-xs-12 col-sm-8">
                                                    <input id="c-place_origin" class="form-control" name="row[place_origin]" type="text"  value="{$row['image']}" disabled>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="control-label col-xs-12 col-sm-2">{:__('Unit')}:</label>
                                                <div class="col-xs-12 col-sm-8">
                                                    <input id="c-unit" class="form-control" name="row[unit]" type="text" value="{$row['image']}" disabled>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="control-label col-xs-12 col-sm-2">{:__('Specs')}:</label>
                                                <div class="col-xs-12 col-sm-8">
                                                    <input id="c-specs" class="form-control" name="row[specs]" value="{$row['specs']}" type="text" disabled>
                                                </div>
                                            </div>

                                        </div>
                                </fieldset>
                                </div>
                            </div>
                            <hr>
                            <span>物料组成</span>
                            <span style="color: red">未避免数据交叉干扰，多仓库情况下默认只展示第一仓库的库存和成本价</span>
                             <div class="fixed-table-toolbar"><div class="bs-bars pull-left"><div id="toolbar" class="toolbar" style="position: relative;">
                                   <a href="javascript:;" class="btn btn-success btn-add " title="添加"><i class="fa fa-plus"></i> 添加</a>
                             </div>
                        </div>
                       <div class="columns-right pull-right" style="margin-top:10px;margin-bottom:10px;">
                    </div>
                        <table id="table" class="table table-striped table-bordered table-hover table-nowrap"  data-operate-edit="1" data-operate-del="1" width="100%">
                            <thead>
                            <tr>
                                <th style="text-align: center; vertical-align: middle; " data-field="id"><div class="th-inner ">Id</div></th>
<!--                                <th style="text-align: center; vertical-align: middle; " data-field="createtime"><div class="th-inner ">出库仓库</div></th>-->
                                <th style="text-align: center; vertical-align: middle; " ><div class="th-inner ">{:__('Title')}</div></th>
                                <th style="text-align: center; vertical-align: middle; " data-field="operator"><div class="th-inner ">{:__('Brands_id')}</div></th>
                                <th style="text-align: center; vertical-align: middle; " data-field="company"><div class="th-inner ">{:__('Place_origin')}</div></th>
                                <th style="text-align: center; vertical-align: middle; " data-field="number"><div class="th-inner ">{:__('Unit')}</div></th>
                                <th style="text-align: center; vertical-align: middle; " data-field="supplier_id"><div class="th-inner ">{:__('Specs')}</div></th>
                                <th style="text-align: center; vertical-align: middle; " data-field="supplier_id"><div class="th-inner ">{:__('Num')}</div></th>
                                <th style="text-align: center; vertical-align: middle; " data-field="supplier_id"><div class="th-inner ">{:__('Cost')}</div></th>
                                <th style="text-align: center; vertical-align: middle; " data-field="supplier_id"><div class="th-inner ">{:__('Operate')}</div></th>
                            </tr>
                            </thead>
                            <tbody data-listidx="0" class="connnn">
                                {volist name="materGoods" id="vo"}
                                    <tr data-index="0">
                                        <td style="text-align: center; vertical-align: middle; ">
                                            <input name="cost[{$vo.id}][goods_id]" value="{$vo.id}" style="display: none" class="gid gids{$vo.id}">{$vo.id}
                                        </td>
                                        <td style="text-align: center; vertical-align: middle; ">{$vo.title}</td>
                                        <td style="text-align: center; vertical-align: middle; ">{$vo.name}</td>
                                        <td style="text-align: center; vertical-align: middle; ">{$vo.place_origin}</td>
                                        <td style="text-align: center; vertical-align: middle; " class="title">{$vo.unit}</td>
                                        <td style="text-align: center; vertical-align: middle; ">{$vo.specs}</td>
                                        <td style="text-align: center; vertical-align: middle; " class="xxgg">
                                            <input type="text" onkeyup="onlynumber(this);" style="display: none" class="requisition_num" name="cost[{$vo.id}][goods_num]" value="{$vo.material_num}">
                                            <span class=" ">{$vo.material_num}</span>
                                        </td>
                                        <td style="text-align: center; vertical-align: middle; " class="">
                                            <input type="text" class="form-control new_cost_price" style="display: none" value="{$vo.new_cost_price}" name="cost[{$vo.id}][new_cost_price]">
                                            <span class="new_cost_price">{$vo.new_cost_price}</span>
                                        </td>
                                        <td style="text-align: center; vertical-align: middle; ">
                                            <a href="javascript:;" class="btn btn-xs btn-danger btn-delone-cost" data-toggle="tooltip" title="" data-table-id="table" data-field-index="15" data-row-index="0" data-button-index="2" data-original-title="删除">
                                                <i class="fa fa-trash"></i>
                                            </a>
                                        </td>
                                    </tr>
                                {/volist}
                            </tbody>

                            <tbody class="tbfoot" data-listidx="0" style="display: none">
                                <td style="text-align: center; vertical-align: middle; ">合计</td>
                                <td style="text-align: center; vertical-align: middle; "></td>
                                <td style="text-align: center; vertical-align: middle; "></td>
                                <td style="text-align: center; vertical-align: middle; "></td>
                                <td style="text-align: center; vertical-align: middle; " ></td>
                                <td style="text-align: center; vertical-align: middle; " ></td>
                                <td style="text-align: center; vertical-align: middle; " class="allnum">0</td>
                                <td style="text-align: center; vertical-align: middle; " ></td>
                                <td style="text-align: center; vertical-align: middle; " ></td>
                            </tbody>
                        </table>
                    </div>
                        <br>
                        <div class="widget-body no-padding">

                            <div class="bootstrap-table">
                                <div class="commonsearch-table ">
                                    <fieldset>
                                        <div class="row">
                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                                <label for="c-actual_goods_cost" class="control-label col-xs-4">{:__('EstimatedCost')}</label>
                                                <div class="col-xs-8">
                                                    <input id="c-actual_goods_cost" data-rule="required"  class="form-control actual_goods_cost" name="actual-cost" type="text" value="{$allcost}" disabled>
                                                    <input  data-rule="required"  class="form-control actual_goods_cost" name="actual_cost" type="text" value="{$allcost}" style="display: none">
                                                </div>
                                            </div>
                                        </div>
                                </fieldset>
                                </div>
                            </div>
                        </div>

                </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <input type="hidden" name="draft" value="0" class="draft">
            <button type="submit" class="btn btn-success btn-embossed subpurchase">{:__('OK')}</button>
        </div>
    </div>

</form>
<script type="text/html" id="customtpl">
    <li class="col-xs-3">
        <a href="<%=fullurl%>" data-url="<%=url%>" target="_blank" class="thumbnail"><img src="<%=fullurl%>" class="img-responsive"></a>
    </li>
</script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
    is_edit = 0;

    var tax = 0;
    function getrequisition(sid){
        if($('input').is('.gids'+sid)){
            layer.msg('该商品已添加')
            return false;
        }
        var url = "{$turl}"
        $.post(url+'/goods/goods/getgood',{ids:sid},function (res) {
            if(res.code == 9){
                layer.msg(res.msg)
                return  false;
            }
            if(res.code == 0){
                layer.msg(res.msg)
                return  false;
            }
            if(res != null){
                f(res)
            }else{
                layer.msg('数据异常')
                return false;
            }

        })
    }
    function f(res) {
        $('.tbfoot').attr('style','display:')
        var str = '';
            str += '<tr data-index="0">\n'
            str += ' <td style="text-align: center; vertical-align: middle; ">' +
                '<input name="cost['+res.id+']['+'goods_id'+']" value="'+res.id+'" style="display: none" class="gid gids'+res.id+'">' +
                ''+res.id+'</td>\n' +
                ' <td style="text-align: center; vertical-align: middle; ">'+res.title+'</td>\n' +
                ' <td style="text-align: center; vertical-align: middle; ">'+res.stock+'</td>\n' +
                ' <td style="text-align: center; vertical-align: middle; ">'+res.place_origin+'</td>\n' +
                ' <td style="text-align: center; vertical-align: middle; " class="title">'+res.unit+'</td>\n' +
                ' <td style="text-align: center; vertical-align: middle; ">'+res.specs+'</td>\n';

           str +=
               ' <td style="text-align: center; vertical-align: middle; " class="xxgg">' +
               '<input type="text" onkeyup="onlynumber(this);" style="display: none" class="requisition_num" name="cost['+res.id+']['+'goods_num'+']" value="1">'+
               '<span class=" ">1</span></td>\n' +

               ' <td style="text-align: center; vertical-align: middle; " class="">\n' +
                '                                    <input type="text" class="form-control new_cost_price" style="display: none" value="'+res.new_cost_price+'" name="cost['+res.id+'][new_cost_price]">\n' +
                '                                    <span class="new_cost_price">'+res.new_cost_price+'</span>\n' +
                '                                </td>\n' +
               ' <td style="text-align: center; vertical-align: middle; ">\n' +
               ' <a href="javascript:;" class="btn btn-xs btn-danger btn-delone-cost" data-toggle="tooltip" title="" data-table-id="table" data-field-index="15" data-row-index="0" data-button-index="2" data-original-title="删除"><i class="fa fa-trash"></i></a></td>\n' +
                '</tr>'
        $('.connnn').append(str);
           console.log("____________________________")
        costCount()
    }

    var is_check = 0;
    var is_sure = false;
    $('.subpurchase').click(function () {
        if($('.connnn').find("tr").length<1 && is_sure == false){
            layer.confirm('您确定不设置物料商品吗？', function(index){
                //用户点击了确认按钮
                layer.close(index);
                console.log("我确定了")
                is_sure = true
                $('.subpurchase').click()
                //执行相关操作
            }, function(index){
                //用户点击了取消按钮
                layer.close(index);
                return  false;
            });
            return  false;
        }

        $('.connnn').find("tr").each(function(){
            var anum = $(this).find('.requisition_num').val()
            // console.log(anum)
            if(anum < 1 ){
                noticename += $(this).find('.title').html() +'&nbsp'
            }
        });
        console.log('问题商品:'+noticename)
        if(noticename != ''){
            layer.msg(noticename+'未设置数量？')
            is_sure = false
            return false;
        }else{

        }
    })

    $('.subpurchases').click(function () {
        $('.draft').val(1)
        if(is_check == 0){
            var noticename = '';
            if($('.actual_cost').val() <0){
                layer.msg('实际费用不能低于0')
                return false
            }
            if($('.connnn').find("tr").length<1){
                layer.msg('没有选择商品111')
                return  false;
            }
            $('.connnn').find("tr").each(function(){
                var anum = $(this).find('.requisition_num').val()
                if(anum < 1 ){
                    noticename += $(this).find('.title').html() +'&nbsp'
                }
            });
            if(noticename != ''){
                layer.msg(noticename+'未设置数量？')
                return false;
            }else{

            }
        }
    })
    $('body').on('click','.btn-caigou',function () {
        var gid = $(this).parents('tr').find('.gid').val()
        var url = "{$turl}"
        layer.open({
            type: 2, // 1-在content中写html代码， 2-在content中写src路径
            area: ['50%', '90%'],
            title: "采购商品",
            content: url+'/goods/goods/addpgoods?ids='+gid,
            // content: Config.this_url+'/purchase/requisition',
        })
    })
    function costCount(){
        if($(".connnn > tr").length == 0){
            $('.tbfoot').attr('style','display:none')
            // return false;
        }else{
            $('.tbfoot').attr('style','display:')
        }
        all_cost = 0
        all_num = 0
        $('.connnn').find("tr").each(function(){
            num = $(this).find('.requisition_num').val()*1//数量
            all_num = all_num + num
            htprice = $(this).find('.new_cost_price').prev().val()*1//成本
            if(isNaN(htprice) || htprice == 0){
                return true
            }
            console.log("成本价")
            console.log(num)
            console.log(htprice)

            goods_cost = num*htprice

            cost = goods_cost.toFixed(4)
            console.log(cost)
            console.log("成本价")
            all_cost = all_cost*1 + cost*1

        });
        console.log(all_cost)
        console.log("数量",all_num)
        $('.actual_goods_cost').val(all_cost)
        $('.allnum').html(all_num)

    }
    $('.connnn').on('blur','input',function () {
        costCount()
    })
    $('.connnn').on('click','.btn-delone-cost',function () {
        $(this).parents('tr').remove()
        console.log('成本重新计算')
        costCount()
    })

</script>
{include file='common/footer' /}